﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MyVoteMVC.BackstageManagement.Controllers;
using MyVoteMVC.Domain;

namespace MyVoteMVC.BackstageManagement.Views
{
    public partial class UpdateQuestion : Form, IQuestionView
    {
        public event EventHandler OnUpdateComplete;

        private IQuestionController controller;

        private string activityId;

        private string questionId;

        public UpdateQuestion(string activityId,string questionId)
        {
            InitializeComponent();
            controller = new QuestionController(this);
            this.activityId = activityId;
            this.questionId = questionId;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
            
        }

        private void AddActivity_FormClosing(object sender, FormClosingEventArgs e)
        {
            OnUpdateComplete(null, null);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (cbMultiItems.Checked && numMutilCount.Value <= 0)
            {
                MessageBox.Show("多选数目必须大于1");
                return;
            }

            controller.EditQuestion(
                activityId,
                questionId,
                txtTitle.Text,
                txtContent.Text,
                (cbMultiItems.Checked ? (numMutilCount.Value == 0 ? 1 : (int)numMutilCount.Value) : 1),
                (cbMustAnswer.Checked ? 1 : 0),
                (cbMultiItems.Checked ? 1 : 0),
                (cbStatus.Checked ? 1 : 0)
                );
        }

        private void UpdateQuestion_Load(object sender, EventArgs e)
        {
            numMutilCount.Maximum = int.MaxValue;
            controller.GetQuestion(questionId);
        }

        public void CreateQuestionComplete(string message)
        {
            throw new NotImplementedException();
        }

        public void EditQuestionComplete(string message)
        {
            MessageBox.Show(message);
        }

        public void GetQuestionComplete(VT_Questions question)
        {
            txtContent.Text = question.Question_Content;
            txtTitle.Text = question.Question_Title;
            cbMultiItems.Checked = (question.Question_IfMultiItems == 1 ? true : false);
            cbMustAnswer.Checked = (question.Question_IfMustAnswer == 1 ? true : false);
            cbStatus.Checked = (question.Question_Status == 1 ? true : false);
            numMutilCount.Value = cbMultiItems.Checked ? (question.MultiCount.HasValue ? question.MultiCount.Value : 0) : 0;
        }

        private void cbMultiItems_CheckedChanged(object sender, EventArgs e)
        {
            pMutilCount.Visible = cbMultiItems.Checked;
        }

        public void OnSubmitError(string message)
        {
            MessageBox.Show(message);
        }
    }
}
